/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.fca.daos;

import db.dao.AbstractDAO;
import db.orm.BeansGenerator;
import edu.fca.beans.AlumnoBean;
import java.util.ArrayList;

/**
 *
 * @author Romms
 */
public class AlumnoDAO extends AbstractDAO<AlumnoBean>{

    @Override
    public void add(AlumnoBean entity) throws Exception {
        super.setQuery("INSERT INTO alumno (" +
            "matricula," +
            "apellidoP," +
            "apellidoM,"+
            "nombre,"+
            "lic) " +
            "VALUES" + "('"+ entity.getMatricula()+ "','" +
            entity.getApellidoP()+ "','" +
            entity.getApellidoM()+ "','" +
            entity.getNombre()+ "','" +
            entity.getLic()+ "')");
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    public void deleteWhere(String condition) throws Exception {
        super.setQuery("DELETE FROM alumno WHERE " + condition);
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    public void updateWhere(AlumnoBean entity,String condition) throws Exception {
        super.setQuery("UPDATE alumno SET " +
            "matricula='"+entity.getMatricula()+
             "',apellidoP='"+ entity.getApellidoP()+
             "',apellidoM='"+ entity.getApellidoM()+
             "',nombre='"+ entity.getNombre()+
             "',lic='"+ entity.getLic()+
             "' WHERE "+ condition);
        super.connectionManager.getConnection().executeUpdate(super.getQuery());
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
    }

    @Override
    protected ArrayList<AlumnoBean> select() throws Exception {
        ArrayList<AlumnoBean> list  =BeansGenerator.getInstance().generateListOfBeans
                (super.connectionManager.getConnection().executeQuery(this.getQuery()), AlumnoBean.class);
        this.connectionManager.closeDbConnection(super.connectionManager.getConnection());
        return list;
    }

    @Override
    public ArrayList<AlumnoBean> selectAll() throws Exception {
        this.setQuery("SELECT * FROM alumno");
        return this.select();
    }

    @Override
    public ArrayList<AlumnoBean> selectWhere(String condition) throws Exception {
        this.setQuery("SELECT * FROM alumno WHERE "+condition);
        return this.select();
    }

}
